using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKAERG
    {
        public DataRow Convert2KAERG(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKAERG(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KAEMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KAEKANR"] = " ".PadRight(8 ); 
         dwReturn["KAEKAPOS"] = " ".PadRight(4 ); 
         dwReturn["KAEVPOS"] = " ".PadRight(2 ); 
         dwReturn["KAEHGR"] = " ".PadRight(1 ); 
         dwReturn["KAEUGR"] = " ".PadRight(3 ); 
         dwReturn["KAEPOSNR"] = " ".PadRight(3 ); 
         dwReturn["KAEZUS"] = " ".PadRight(2 ); 
         dwReturn["KAEKALKBEN"] = " ".PadRight(8 ); 
         dwReturn["KAEKALKDAT"] = " ".PadRight(0 ); 
         dwReturn["KAEKOSTEN"] = " ".PadRight(0 ); 
         dwReturn["KAEKOSA"] = " ".PadRight(0 ); 
         dwReturn["KAEME"] = " ".PadRight(1 ); 
         dwReturn["KAEKZPROZ"] = " ".PadRight(1 ); 
         dwReturn["KAEMAT"] = " ".PadRight(0 ); 
         dwReturn["KAECHAR1"] = " ".PadRight(0 ); 
         dwReturn["KAECHAR2"] = " ".PadRight(0 ); 
         dwReturn["KAECHAR3"] = " ".PadRight(0 ); 
         dwReturn["KAECHAR4"] = " ".PadRight(0 ); 
         dwReturn["KAECHAR5"] = " ".PadRight(0 ); 
         dwReturn["KAESOLLDB"] = " ".PadRight(0 ); 
         dwReturn["KAEMENGE"] = " ".PadRight(0 ); 
         dwReturn["KAEVART"] = " ".PadRight(5 ); 
         dwReturn["KAEMERKM"] = " ".PadRight(8 ); 
         dwReturn["KAEMEAEND"] = " ".PadRight(1 ); 
         dwReturn["KAEKSAEND"] = " ".PadRight(1 ); 
         dwReturn["KAEFILLER"] = " ".PadRight(4 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKAERG(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKAERG(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKAERG(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKAERG(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKAERG(dwLocal, ITXDBVERSION);
            }
            UpdateKAERG(dwLocal);
        }
        public void InsertKAERG(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KAERG(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKAERG(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KAERG WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KAERG  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KAERG";
            Return = Result;
            return Return;
        }
        public void DeleteKAERG(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KAERG   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKAERG(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KAERG SET "
                    +"KAEMANDANT= :KAEMANDANT,"
                    +"KAEKANR= :KAEKANR,"
                    +"KAEKAPOS= :KAEKAPOS,"
                    +"KAEVPOS= :KAEVPOS,"
                    +"KAEHGR= :KAEHGR,"
                    +"KAEUGR= :KAEUGR,"
                    +"KAEPOSNR= :KAEPOSNR,"
                    +"KAEZUS= :KAEZUS,"
                    +"KAEKALKBEN= :KAEKALKBEN,"
                    +"KAEKALKDAT= :KAEKALKDAT,"
                    +"KAEKOSTEN= :KAEKOSTEN,"
                    +"KAEKOSA= :KAEKOSA,"
                    +"KAEME= :KAEME,"
                    +"KAEKZPROZ= :KAEKZPROZ,"
                    +"KAEMAT= :KAEMAT,"
                    +"KAECHAR1= :KAECHAR1,"
                    +"KAECHAR2= :KAECHAR2,"
                    +"KAECHAR3= :KAECHAR3,"
                    +"KAECHAR4= :KAECHAR4,"
                    +"KAECHAR5= :KAECHAR5,"
                    +"KAESOLLDB= :KAESOLLDB,"
                    +"KAEMENGE= :KAEMENGE,"
                    +"KAEVART= :KAEVART,"
                    +"KAEMERKM= :KAEMERKM,"
                    +"KAEMEAEND= :KAEMEAEND,"
                    +"KAEKSAEND= :KAEKSAEND,"
                    +"KAEFILLER= :KAEFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KAEMANDANT", OracleType.NVarChar).Value = dwData["KAEMANDANT"].ToString(); 
          or1.Parameters.Add("KAEKANR", OracleType.NVarChar).Value = dwData["KAEKANR"].ToString(); 
          or1.Parameters.Add("KAEKAPOS", OracleType.NVarChar).Value = dwData["KAEKAPOS"].ToString(); 
          or1.Parameters.Add("KAEVPOS", OracleType.NVarChar).Value = dwData["KAEVPOS"].ToString(); 
          or1.Parameters.Add("KAEHGR", OracleType.NVarChar).Value = dwData["KAEHGR"].ToString(); 
          or1.Parameters.Add("KAEUGR", OracleType.NVarChar).Value = dwData["KAEUGR"].ToString(); 
          or1.Parameters.Add("KAEPOSNR", OracleType.NVarChar).Value = dwData["KAEPOSNR"].ToString(); 
          or1.Parameters.Add("KAEZUS", OracleType.NVarChar).Value = dwData["KAEZUS"].ToString(); 
          or1.Parameters.Add("KAEKALKBEN", OracleType.NVarChar).Value = dwData["KAEKALKBEN"].ToString(); 
          or1.Parameters.Add("KAEKALKDAT", OracleType.NVarChar).Value = dwData["KAEKALKDAT"].ToString(); 
          or1.Parameters.Add("KAEKOSTEN", OracleType.NVarChar).Value = dwData["KAEKOSTEN"].ToString(); 
          or1.Parameters.Add("KAEKOSA", OracleType.NVarChar).Value = dwData["KAEKOSA"].ToString(); 
          or1.Parameters.Add("KAEME", OracleType.NVarChar).Value = dwData["KAEME"].ToString(); 
          or1.Parameters.Add("KAEKZPROZ", OracleType.NVarChar).Value = dwData["KAEKZPROZ"].ToString(); 
          or1.Parameters.Add("KAEMAT", OracleType.NVarChar).Value = dwData["KAEMAT"].ToString(); 
          or1.Parameters.Add("KAECHAR1", OracleType.NVarChar).Value = dwData["KAECHAR1"].ToString(); 
          or1.Parameters.Add("KAECHAR2", OracleType.NVarChar).Value = dwData["KAECHAR2"].ToString(); 
          or1.Parameters.Add("KAECHAR3", OracleType.NVarChar).Value = dwData["KAECHAR3"].ToString(); 
          or1.Parameters.Add("KAECHAR4", OracleType.NVarChar).Value = dwData["KAECHAR4"].ToString(); 
          or1.Parameters.Add("KAECHAR5", OracleType.NVarChar).Value = dwData["KAECHAR5"].ToString(); 
          or1.Parameters.Add("KAESOLLDB", OracleType.NVarChar).Value = dwData["KAESOLLDB"].ToString(); 
          or1.Parameters.Add("KAEMENGE", OracleType.NVarChar).Value = dwData["KAEMENGE"].ToString(); 
          or1.Parameters.Add("KAEVART", OracleType.NVarChar).Value = dwData["KAEVART"].ToString(); 
          or1.Parameters.Add("KAEMERKM", OracleType.NVarChar).Value = dwData["KAEMERKM"].ToString(); 
          or1.Parameters.Add("KAEMEAEND", OracleType.NVarChar).Value = dwData["KAEMEAEND"].ToString(); 
          or1.Parameters.Add("KAEKSAEND", OracleType.NVarChar).Value = dwData["KAEKSAEND"].ToString(); 
          or1.Parameters.Add("KAEFILLER", OracleType.NVarChar).Value = dwData["KAEFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
